package com.sc.nft.helper;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class MessageQueueHelper {
    @Value("${spring.profiles.active}")
    private String active;

    private final RocketMQTemplate rocketMQTemplate;

    public void sendMessage(String topic,Object body){
        String sendTopic = topic.concat(active);
        log.info("send message topic:{},body:{}",topic,body);
        rocketMQTemplate.convertAndSend(sendTopic,body);
    }

    public void sendMessage(String topic,Object body,String tag){
        String sendTopic = topic.concat(active).concat(":").concat(tag);
        log.info("send message topic:{},body:{}",topic,body);
        rocketMQTemplate.convertAndSend(sendTopic,body);
    }

    public void sendMessage(String topic,Object body,String tag,String key){
        String sendTopic = topic.concat(active).concat(":").concat(tag);
        SendResult sendResult = rocketMQTemplate.syncSend(sendTopic,
                MessageBuilder.withPayload(body)
                        .setHeader(MessageConst.PROPERTY_KEYS, key)   // 指定业务key
                        .build());
        log.info("topic:{},body:{},tag:{},key:{},sendResult:{}",topic,body,tag,key,sendResult);

    }

    public void sendMessageNotActive(String topic,Object body,String tag){
        rocketMQTemplate.convertAndSend(topic,body);
    }

}
